#include "thriftServer.h"


using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::stdcxx;

ThriftServer::ThriftServer(QObject *parent) : QThread(parent)
{
//    handler = new ThriftHandler(parent);
    m_isRun = true;
    this->start();
}

void ThriftServer::disableRun()
{
    m_isRun = false;
}

void ThriftServer::run()
{
    int port = 9090;
#if 1
    ::apache::thrift::stdcxx::shared_ptr<ThriftHandler> handler(new ThriftHandler(parent()));
    ::apache::thrift::stdcxx::shared_ptr<TProcessor> processor(new ThriftProcessor(handler));
    ::apache::thrift::stdcxx::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    ::apache::thrift::stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    ::apache::thrift::stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

#if 0
    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    server.serve();
#else
    TSimpleServer *server = new TSimpleServer(processor, serverTransport, transportFactory, protocolFactory);
    qDebug()<<"server->serve()";
    server->serve();

#endif
#else
    shared_ptr<ThriftHandler> handler(new ThriftHandler());
    shared_ptr<TProcessor> processor(new RegistProcessor(handler));
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);
    shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory > (new PosixThreadFactory());
    threadManager->threadFactory(threadFactory);
    threadManager->start();
    TNonblockingServer server(processor, protocolFactory, port, threadManager);
    server.serve();
#endif

    qDebug()<<"Enter here to indicate that an error has occurred.";
    while(m_isRun)
    {
    }
}
